package com.alibaba.citrus.ots.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CalendarCompareUtil {
/**
 * 根据当前时间进行比较大小
 *
 * @param
 * @return
 */
public static boolean compareDateTimes(String startTime,String endTime){
    GregorianCalendar gregorianCalendar=new GregorianCalendar();
    gregorianCalendar.setTime(new Date());
    int hour = gregorianCalendar.get(Calendar.HOUR_OF_DAY);
    int min = gregorianCalendar.get(Calendar.MINUTE);
    int second = gregorianCalendar.get(Calendar.SECOND);
    String hour_val = String.valueOf(hour);
    String min_val = String.valueOf(min);
    String second_val = String.valueOf(second);
    String nowTime=hour_val+":"+min_val+":"+second_val;
    if(StringUtils.isNotEmpty(startTime)) {
        boolean start = compTime(nowTime,startTime );
        boolean end = compTime(nowTime, endTime);
        if (start && !end) {
            return true;
        }
    }else if(StringUtils.isNotEmpty(endTime)){
       return compTime(nowTime,endTime);
    }
    return false;
}
    public static boolean compTime(String v1,String v2){
        try{
            if(v1.indexOf(":")<0||v2.indexOf(":")<0){
                System.out.println("格式不正确！");
            }else{
                String[] array=v1.split(":");
                Integer total1= Integer.valueOf(array[0])*3600+Integer.valueOf(array[1])*60+Integer.valueOf(array[2]);
                String[] array1=v2.split(":");
                Integer total2= Integer.valueOf(array1[0])*3600+Integer.valueOf(array1[1])*60+Integer.valueOf(array1[2]);
                return total1-total2>0?true:false;
            }
        }catch(NumberFormatException e){
            return false;
        }
        return false;
    }
}
